using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ZedGraph;

namespace ICP_MS.Plugin.Experiment;

public class FormSemiCorrect : Form
{
	private List<IsotopeInfos> isotopes = new List<IsotopeInfos>();

	private List<IsotopeCurve> curves = new List<IsotopeCurve>();

	private IContainer components = null;

	private ContextMenuStrip contextMenuStrip1;

	private ZedGraphControl graph;

	private TableLayoutPanel tableLayoutPanel1;

	private GroupBox groupBox1;

	private DataGridView dgvNew;

	private Button btnCalculate;

	private Button btnClose;

	private Button btnApply;

	private GroupBox groupBox3;

	private System.Windows.Forms.Label lblR;

	private System.Windows.Forms.Label label1;

	private Panel panel1;

	private DataGridViewTextBoxColumn colSymbol;

	private DataGridViewTextBoxColumn colMass;

	private DataGridViewTextBoxColumn colFactor;

	private DataGridViewTextBoxColumn ColCaliFactor;

	private DataGridViewTextBoxColumn ColRSF;

	private DataGridViewCheckBoxColumn ColCorrect;

	private DataGridViewCheckBoxColumn ColIsVisible;

	public FormSemiCorrect(List<IsotopeCurve> curves, List<IsotopeInfos> isotopes)
	{
		InitializeComponent();
		this.curves = curves;
		this.isotopes = isotopes;
	}

	private void FormSemiCorrect_Load(object sender, EventArgs e)
	{
		GraphPane pane = graph.GraphPane;
		pane.Title.Text = "";
		pane.XAxis.Title.Text = "amu";
		pane.YAxis.Title.Text = "factor";
		SemiCalculator.Calculator.SemiIsotopes.Clear();
		ApplyResources();
		dgvNew.AutoGenerateColumns = false;
		BindNew();
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormSemiCorrect.Title.Text");
		groupBox1.Text = Lang.Txt("FormSemiCorrect.NewCoffient.Text");
		groupBox3.Text = Lang.Txt("FormSemiCorrect.LinearCoff.Text");
		btnApply.Text = Lang.Txt("FormSemiCorrect.Apply.Text");
		btnCalculate.Text = Lang.Txt("FormSemiCorrect.Calculate.Text");
		btnClose.Text = Lang.Txt("FormSemiCorrect.Close.Text");
		colMass.HeaderText = Lang.Txt("FormSemiCorrect.Columns.Mass.Text");
		colSymbol.HeaderText = Lang.Txt("FormSemiCorrect.Columns.Element.Text");
		colFactor.HeaderText = Lang.Txt("AddFormSemiCorrect.ActualSen");
		ColCaliFactor.HeaderText = Lang.Txt("AddFormSemiCorrect.CORRSen");
		ColCorrect.HeaderText = Lang.Txt("AddFormSemiCorrect.Calibration");
		ColIsVisible.HeaderText = Lang.Txt("AddFormSemiCorrect.UpdateDatabase");
	}

	private void BindNew()
	{
		foreach (IsotopeCurve curve in curves)
		{
			if (curve.calculated)
			{
				SemiCalculator.Calculator.SemiIsotopes.Add(new SemiIsotope
				{
					IsotopeId = curve.isotopeId,
					Symbol = curve.symbol,
					Mass = (from isotope in isotopes
						where isotope.symbol == curve.symbol
						select isotope.mass).First(),
					Factor = Math.Round(curve.k, 2),
					Visible = (Math.Round(curve.k, 2) > 0.0),
					Correct = false
				});
				SemiCalculator.Calculator.SemiIsotopes.Sort((SemiIsotope s1, SemiIsotope s2) => s1.Mass.CompareTo(s2.Mass));
			}
		}
		dgvNew.DataSource = SemiCalculator.Calculator.SemiIsotopes;
	}

	private void btnApply_Click(object sender, EventArgs e)
	{
		if (MessageBox.Show(Lang.Txt("AddFormSemiCorrect.UpdateDatabase") + "？", Lang.Txt("AddFormSemiCorrect.Warn"), MessageBoxButtons.YesNo) == DialogResult.No)
		{
			return;
		}
		if (SemiCalculator.Calculator.Curve.R < 0.9)
		{
			MessageBox.Show(Lang.Txt("AddFormSemiCorrect.GenerateCorrectCurve"), Lang.Txt("AddFormSemiCorrect.Warn"));
			return;
		}
		foreach (SemiIsotope semi in SemiCalculator.Calculator.SemiIsotopes)
		{
			SemiIsotope isotope = SemiCalculator.Calculator.RSFIsotopes.Find((SemiIsotope r) => r.Mass == semi.Mass && r.Symbol == semi.Symbol);
			if (isotope != null)
			{
				isotope.RSF = (semi.Visible ? semi.RSF : isotope.RSF);
				isotope.CaliFactor = semi.CaliFactor;
			}
			else if (semi.Visible)
			{
				SemiCalculator.Calculator.RSFIsotopes.Add(semi);
			}
		}
		foreach (SemiIsotope rsf in SemiCalculator.Calculator.RSFIsotopes)
		{
			rsf.CaliFactor = Math.Round(SemiCalculator.Calculator.Curve.a0 + SemiCalculator.Calculator.Curve.a1 * rsf.Mass + SemiCalculator.Calculator.Curve.a2 * rsf.Mass * rsf.Mass, 2);
		}
		dgvNew.Refresh();
	}

	private void btnCalculate_Click(object sender, EventArgs e)
	{
		SemiCalculator.Calculator.CalculateCurve();
		lblR.Text = SemiCalculator.Calculator.Curve.R.ToString("F6");
		foreach (SemiIsotope semi2 in SemiCalculator.Calculator.SemiIsotopes)
		{
			semi2.CaliFactor = Math.Round(SemiCalculator.Calculator.Curve.a0 + semi2.Mass * SemiCalculator.Calculator.Curve.a1 + SemiCalculator.Calculator.Curve.a2 * semi2.Mass * semi2.Mass, 2);
			semi2.RSF = Math.Round(semi2.CaliFactor / semi2.Factor, 2);
		}
		graph.GraphPane.CurveList.Clear();
		List<double> mass1 = new List<double>();
		List<double> mass2 = new List<double>();
		List<double> mass3 = new List<double>();
		List<double> k1 = new List<double>();
		List<double> k2 = new List<double>();
		List<double> k3 = new List<double>();
		foreach (SemiIsotope semi in SemiCalculator.Calculator.SemiIsotopes)
		{
			if (semi.Correct)
			{
				mass1.Add(semi.Mass);
				k1.Add(semi.Factor);
			}
			else
			{
				mass3.Add(semi.Mass);
				k3.Add(semi.Factor);
			}
			mass2.Add(semi.Mass);
			k2.Add(SemiCalculator.Calculator.Curve.a0 + SemiCalculator.Calculator.Curve.a1 * semi.Mass + SemiCalculator.Calculator.Curve.a2 * semi.Mass * semi.Mass);
		}
		LineItem line0 = graph.GraphPane.AddCurve("", mass1.ToArray(), k1.ToArray(), Color.Blue, SymbolType.Diamond);
		line0.Line.IsVisible = false;
		line0.Symbol.Fill.Type = FillType.Solid;
		LineItem line1 = graph.GraphPane.AddCurve("", mass2.ToArray(), k2.ToArray(), Color.Blue, SymbolType.None);
		line1.Symbol.Fill.Type = FillType.Solid;
		LineItem line2 = graph.GraphPane.AddCurve("", mass3.ToArray(), k3.ToArray(), Color.Red, SymbolType.Circle);
		line2.Line.IsVisible = false;
		line2.Symbol.Fill.Type = FillType.Solid;
		graph.AxisChange();
		graph.Refresh();
		dgvNew.Refresh();
	}

	private void btnClose_Click(object sender, EventArgs e)
	{
		SemiCalculator.Calculator.Save();
		Close();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
		this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
		this.graph = new ZedGraph.ZedGraphControl();
		this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.dgvNew = new System.Windows.Forms.DataGridView();
		this.colSymbol = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colMass = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colFactor = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColCaliFactor = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColRSF = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColCorrect = new System.Windows.Forms.DataGridViewCheckBoxColumn();
		this.ColIsVisible = new System.Windows.Forms.DataGridViewCheckBoxColumn();
		this.panel1 = new System.Windows.Forms.Panel();
		this.btnCalculate = new System.Windows.Forms.Button();
		this.btnClose = new System.Windows.Forms.Button();
		this.btnApply = new System.Windows.Forms.Button();
		this.groupBox3 = new System.Windows.Forms.GroupBox();
		this.lblR = new System.Windows.Forms.Label();
		this.label1 = new System.Windows.Forms.Label();
		this.tableLayoutPanel1.SuspendLayout();
		this.groupBox1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.dgvNew).BeginInit();
		this.panel1.SuspendLayout();
		this.groupBox3.SuspendLayout();
		base.SuspendLayout();
		this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
		this.contextMenuStrip1.Name = "contextMenuStrip1";
		this.contextMenuStrip1.Size = new System.Drawing.Size(61, 4);
		this.graph.Location = new System.Drawing.Point(11, 10);
		this.graph.Margin = new System.Windows.Forms.Padding(5, 4, 5, 4);
		this.graph.Name = "graph";
		this.graph.ScrollGrace = 0.0;
		this.graph.ScrollMaxX = 0.0;
		this.graph.ScrollMaxY = 0.0;
		this.graph.ScrollMaxY2 = 0.0;
		this.graph.ScrollMinX = 0.0;
		this.graph.ScrollMinY = 0.0;
		this.graph.ScrollMinY2 = 0.0;
		this.graph.Size = new System.Drawing.Size(1089, 315);
		this.graph.TabIndex = 10;
		this.tableLayoutPanel1.ColumnCount = 2;
		this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50f));
		this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 221f));
		this.tableLayoutPanel1.Controls.Add(this.groupBox1, 0, 0);
		this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 0);
		this.tableLayoutPanel1.Location = new System.Drawing.Point(11, 332);
		this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(4);
		this.tableLayoutPanel1.Name = "tableLayoutPanel1";
		this.tableLayoutPanel1.RowCount = 1;
		this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100f));
		this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20f));
		this.tableLayoutPanel1.Size = new System.Drawing.Size(1084, 339);
		this.tableLayoutPanel1.TabIndex = 11;
		this.groupBox1.Controls.Add(this.dgvNew);
		this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.groupBox1.Location = new System.Drawing.Point(4, 4);
		this.groupBox1.Margin = new System.Windows.Forms.Padding(4);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Padding = new System.Windows.Forms.Padding(4);
		this.groupBox1.Size = new System.Drawing.Size(855, 331);
		this.groupBox1.TabIndex = 0;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "新系数";
		this.dgvNew.AllowUserToAddRows = false;
		this.dgvNew.AllowUserToDeleteRows = false;
		dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dgvNew.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
		this.dgvNew.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvNew.Columns.AddRange(this.colSymbol, this.colMass, this.colFactor, this.ColCaliFactor, this.ColRSF, this.ColCorrect, this.ColIsVisible);
		this.dgvNew.Dock = System.Windows.Forms.DockStyle.Fill;
		this.dgvNew.Location = new System.Drawing.Point(4, 22);
		this.dgvNew.Margin = new System.Windows.Forms.Padding(4);
		this.dgvNew.Name = "dgvNew";
		this.dgvNew.RowHeadersVisible = false;
		this.dgvNew.RowTemplate.Height = 23;
		this.dgvNew.Size = new System.Drawing.Size(847, 305);
		this.dgvNew.TabIndex = 0;
		this.colSymbol.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colSymbol.DataPropertyName = "Symbol";
		this.colSymbol.HeaderText = "元素";
		this.colSymbol.Name = "colSymbol";
		this.colSymbol.ReadOnly = true;
		this.colMass.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colMass.DataPropertyName = "Mass";
		this.colMass.HeaderText = "质量数";
		this.colMass.Name = "colMass";
		this.colFactor.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colFactor.DataPropertyName = "Factor";
		this.colFactor.HeaderText = "实际灵敏度";
		this.colFactor.Name = "colFactor";
		this.ColCaliFactor.DataPropertyName = "CaliFactor";
		this.ColCaliFactor.HeaderText = "校正灵敏度";
		this.ColCaliFactor.Name = "ColCaliFactor";
		this.ColCaliFactor.Width = 120;
		this.ColRSF.DataPropertyName = "RSF";
		this.ColRSF.HeaderText = "RSF";
		this.ColRSF.Name = "ColRSF";
		this.ColCorrect.DataPropertyName = "Correct";
		this.ColCorrect.HeaderText = "参与校准";
		this.ColCorrect.Name = "ColCorrect";
		this.ColCorrect.Resizable = System.Windows.Forms.DataGridViewTriState.True;
		this.ColCorrect.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
		this.ColIsVisible.DataPropertyName = "Visible";
		this.ColIsVisible.HeaderText = "更新数据库";
		this.ColIsVisible.Name = "ColIsVisible";
		this.ColIsVisible.Resizable = System.Windows.Forms.DataGridViewTriState.True;
		this.ColIsVisible.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
		this.ColIsVisible.Width = 120;
		this.panel1.Controls.Add(this.btnCalculate);
		this.panel1.Controls.Add(this.btnClose);
		this.panel1.Controls.Add(this.btnApply);
		this.panel1.Controls.Add(this.groupBox3);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel1.Location = new System.Drawing.Point(867, 4);
		this.panel1.Margin = new System.Windows.Forms.Padding(4);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(213, 331);
		this.panel1.TabIndex = 2;
		this.btnCalculate.Location = new System.Drawing.Point(55, 102);
		this.btnCalculate.Margin = new System.Windows.Forms.Padding(4);
		this.btnCalculate.Name = "btnCalculate";
		this.btnCalculate.Size = new System.Drawing.Size(100, 29);
		this.btnCalculate.TabIndex = 3;
		this.btnCalculate.Text = "计算";
		this.btnCalculate.UseVisualStyleBackColor = true;
		this.btnCalculate.Click += new System.EventHandler(btnCalculate_Click);
		this.btnClose.Location = new System.Drawing.Point(55, 201);
		this.btnClose.Margin = new System.Windows.Forms.Padding(4);
		this.btnClose.Name = "btnClose";
		this.btnClose.Size = new System.Drawing.Size(100, 29);
		this.btnClose.TabIndex = 2;
		this.btnClose.Text = "关闭";
		this.btnClose.UseVisualStyleBackColor = true;
		this.btnClose.Click += new System.EventHandler(btnClose_Click);
		this.btnApply.Location = new System.Drawing.Point(55, 151);
		this.btnApply.Margin = new System.Windows.Forms.Padding(4);
		this.btnApply.Name = "btnApply";
		this.btnApply.Size = new System.Drawing.Size(100, 29);
		this.btnApply.TabIndex = 1;
		this.btnApply.Text = "应用";
		this.btnApply.UseVisualStyleBackColor = true;
		this.btnApply.Click += new System.EventHandler(btnApply_Click);
		this.groupBox3.Controls.Add(this.lblR);
		this.groupBox3.Controls.Add(this.label1);
		this.groupBox3.Location = new System.Drawing.Point(19, 15);
		this.groupBox3.Margin = new System.Windows.Forms.Padding(4);
		this.groupBox3.Name = "groupBox3";
		this.groupBox3.Padding = new System.Windows.Forms.Padding(4);
		this.groupBox3.Size = new System.Drawing.Size(181, 70);
		this.groupBox3.TabIndex = 0;
		this.groupBox3.TabStop = false;
		this.groupBox3.Text = "线性系数";
		this.lblR.Location = new System.Drawing.Point(55, 21);
		this.lblR.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.lblR.Name = "lblR";
		this.lblR.Size = new System.Drawing.Size(119, 29);
		this.lblR.TabIndex = 1;
		this.lblR.Text = "1.0";
		this.lblR.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(16, 28);
		this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(31, 15);
		this.label1.TabIndex = 0;
		this.label1.Text = "R =";
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(1108, 674);
		base.Controls.Add(this.tableLayoutPanel1);
		base.Controls.Add(this.graph);
		base.Margin = new System.Windows.Forms.Padding(4);
		base.Name = "FormSemiCorrect";
		base.ShowIcon = false;
		this.Text = "半定量校准";
		base.Load += new System.EventHandler(FormSemiCorrect_Load);
		this.tableLayoutPanel1.ResumeLayout(false);
		this.groupBox1.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.dgvNew).EndInit();
		this.panel1.ResumeLayout(false);
		this.groupBox3.ResumeLayout(false);
		this.groupBox3.PerformLayout();
		base.ResumeLayout(false);
	}
}
